package com.lottery.game.common.utils;

import org.apache.commons.lang3.StringUtils;
import org.apache.commons.lang3.math.NumberUtils;

import java.math.BigDecimal;
import java.sql.Timestamp;
import java.text.DecimalFormat;
import java.text.ParseException;
import java.text.SimpleDateFormat;

import lombok.extern.slf4j.Slf4j;

@Slf4j
public class FormatUtils {

	private static final DecimalFormat DECIMAL_FORMAT = new  DecimalFormat("#####0.00");

	public static final String LONG_DATE_FORMAT_STR = "yyyy-MM-dd HH:mm:ss";

	public static ThreadLocal<SimpleDateFormat> LONG_DATE_FORMAT = new ThreadLocal<SimpleDateFormat>() {
		@Override
		protected SimpleDateFormat initialValue() {
			return new SimpleDateFormat(LONG_DATE_FORMAT_STR);
		}
	};

	public static String formatMoney(BigDecimal money){
		return DECIMAL_FORMAT.format(money);
	}

	public static Object stringParseBaseType(String value, String typeName) {
		if (value == null) {// || "null".equals(value)
			return null;
		}
		if ("double".equals(typeName) || "java.lang.Double".equals(typeName)
				|| "Double".equals(typeName)) {
			if(!NumberUtils.isNumber(value)){
				return 0;
			}
			return NumberUtils.createNumber(value).doubleValue();
		}else if("int".equals(typeName) || "java.lang.Integer".equals(typeName)
				|| "Integer".equals(typeName)){
			if(!NumberUtils.isNumber(value)){
				return 0;
			}
			return NumberUtils.createNumber(value).intValue();
		}else if("long".equals(typeName)  || "java.lang.Long".equals(typeName)
				|| "Long".equals(typeName)){
			if(!NumberUtils.isNumber(value)){
				return 0;
			}
			return NumberUtils.createNumber(value).longValue();
		}else if("float".equals(typeName) || "java.lang.Float".equals(typeName)
				|| "Float".equals(typeName)){
			if(!NumberUtils.isNumber(value)){
				return 0;
			}
			return NumberUtils.createNumber(value).floatValue();
		}else if("char".equals(typeName) || "java.lang.Character".equals(typeName)
				|| "Character".equals(typeName)){
			return typeName.charAt(0);
		}else if("byte".equals(typeName)|| "java.lang.Byte".equals(typeName)
				|| "Byte".equals(typeName)){
			return Byte.valueOf(value);
		}else if("short".equals(typeName)|| "java.lang.Short".equals(typeName)
				|| "Short".equals(typeName)){
			return Short.valueOf(value);
		}else if("boolean".equals(typeName)|| "java.lang.Boolean".equals(typeName)
				|| "Boolean".equals(typeName)){
			return Boolean.valueOf(value);
		} else if ("java.math.BigDecimal".equals(typeName) || "BigDecimal".equals(typeName)) {
			if(NumberUtils.isNumber(value)){
				return new BigDecimal(value);
			}
			return null;
		} else if ("java.sql.Timestamp".equals(typeName) || "Timestamp".equals(typeName)) {
			if (StringUtils.isBlank(value)) {
				return null;
			}
			if (NumberUtils.isNumber(value)) {
				return new Timestamp(Long.parseLong(value));
			}
			try {
				return LONG_DATE_FORMAT.get().parse(value);
			} catch (ParseException pe) {
				log.error("反序列化[" + value + "]为Timestamp类型时出错");
			}
			return null;
		}
		return value;
	}
}
